const baseUrl = "https://nideshop.bluej.cn/api/";
const api = {
  index: baseUrl + "index/index",
  catalogIndex: baseUrl + "catalog/index",
  catalogCurrent: baseUrl + "catalog/current",
  loginByWeixin: baseUrl + "auth/loginByWeixin",
  goodsCount: baseUrl + "goods/count",
  goodsList: baseUrl + "goods/list",
  goodsCategory: baseUrl + "goods/category",
  goodsDetail: baseUrl + "goods/detail",
  goodsNew: baseUrl + "goods/new",
  goodsHot: baseUrl + "goods/hot",
  goodsRelated: baseUrl + "goods/related",
  brandList: baseUrl + "brand/list",
  brandDetail: baseUrl + "brand/detail",
  cartIndex: baseUrl + "cart/index",
  cartAdd: baseUrl + "cart/add",
  cartUpdate: baseUrl + "cart/update",
  cartDelete: baseUrl + "cart/delete",
  cartChecked: baseUrl + "cart/checked",
  cartGoodsCount: baseUrl + "cart/goodscount",
  cartCheckout: baseUrl + "cart/checkout",
  orderSubmit: baseUrl + "order/submit",
  payPrepay: baseUrl + "pay/prepay",
  collectList: baseUrl + "collect/list",
  collectAddordelete: baseUrl + "collect/addordelete",
  commentList: baseUrl + "comment/list",
  commentCount: baseUrl + "comment/count",
  commentPost: baseUrl + "comment/post",
  topicList: baseUrl + "topic/list",
  topicDetail: baseUrl + "topic/detail",
  topicRelated: baseUrl + "topic/related",
  searchIndex: baseUrl + "search/index",
  searchResult: baseUrl + "search/result",
  searchHelper: baseUrl + "search/helper",
  searchClearhistory: baseUrl + "search/clearhistory",
  addressList: baseUrl + "address/list",
  addressDetail: baseUrl + "address/detail",
  addressSave: baseUrl + "address/save",
  addressDelete: baseUrl + "address/delete",
  regionList: baseUrl + "region/list",
  orderList: baseUrl + "order/list",
  orderDetail: baseUrl + "order/detail",
  orderCancel: baseUrl + "order/cancel",
  orderExpress: baseUrl + "order/express",
  footprintList: baseUrl + "footprint/list",
  footprintDelete: baseUrl + "footprint/delete"
};

/**
 * 
 * @param {Object} options
 */
function myRequest(options) {
  let storage_token = wx.getStorageSync('token');
  let token = '';
  if(!!storage_token) {
    token = storage_token;
  }
  let {
    url,
    method = 'get',
    data = {},
    success
  } = options;
  wx.request({
    url: url,
    data: data,
    method: method,
    header: {
      'X-Nideshop-Token': token
    },
    success(res) {
      success(res.data)
    },
    fail(err) {
      fail(err)
    }
  })
}

/**
 * 向购物车添加指定规格产品
 * 
 * @param {Number} goodsId 商品ID
 * @param {Number} number 规定取值为 1
 * @param {Number} productId 商品对应产品ID
 */
function addGoods(goodsId, number, productId) {
  myRequest({
    url: api.cartAdd,
    method: 'post',
    data: { goodsId, number, productId},
    success: res => {
      console.log("addGoods", res.data)
    }
  })
}

/**
 * 添加或取消收藏
 * 
 * @param {Number} goodsID 商品ID
 */
function addOrDeleteCollect( goodsID ) {
  myRequest({
    url: api.collectAddordelete,
    method: "post",
    data: {
      typeId: 0,
      valueId: goodsID
    },
    success: res =>{
      console.log(res.data.type)
    }
  })
}

function timestampToTime(timestamp) {
  var date = new Date(timestamp * 1000);
  var Y = date.getFullYear() + '-';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
  var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  return Y + M + D + h + m + s;
}

module.exports = {
  api: api,
  myRequest: myRequest,
  addGoods: addGoods,
  addOrDeleteCollect: addOrDeleteCollect,
  timestampToTime: timestampToTime
}
